define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'backprice/index' + location.search,
                    add_url: 'backprice/add',
                    edit_url: 'backprice/edit',
                    del_url: 'backprice/del',
                    multi_url: 'backprice/multi',
                    import_url: 'backprice/import',
                    do_url: 'backprice/do_status',
                    table: 'system_back_price',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        // {field: 'first_id', title: __('First_id')},
                        // {field: 'second_id', title: __('Second_id')},
                        {field: 'category.name', title: __('First_id'), operate: 'LIKE'},
                        {field: 'second.name', title: __('Second_id'), operate: 'LIKE'},
                        {field: 'level', title: __('Level'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'type', title: __('Type'), searchList: {"1":__('Type 1'),"2":__('Type 2')}, formatter: Table.api.formatter.normal},
                        {field: 'weight', title: __('Weight'), operate:'BETWEEN'},
                        {field: 'end', title: __('End'), operate:'BETWEEN'},
                        {field: 'price', title: __('Price'), operate:'BETWEEN'},
                        {field: 'yprice', title: __('Yprice'), operate:'BETWEEN'},
                        {field: 'sprice', title: __('Sprice'), operate:'BETWEEN'},
                        {field: 'desc', title: __('Desc'),formatter : function(value, row, index, field){
                                return "<span style='display: block;overflow: hidden;text-overflow: ellipsis;white-space:nowrap;' title='" + row.desc + "'>" + value + "</span>";
                            },
                            cellStyle : function(value, row, index, field) {
                                return {
                                    css: {
                                        "white-space": "nowrap",
                                        "text-overflow": "ellipsis",
                                        "overflow": "hidden",
                                        "max-width": "200px"
                                    }
                                };
                            }, operate: false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,buttons:[
                                {
                                    name: "报价",
                                    title: __("报价"),
                                    classname: "btn btn-xs btn-info btn-click",
                                    icon: "fa fa-cny",
                                    text: "",
                                    click: function (e, row) {
                                        // 创建自定义弹出层内容
                                        var content = '<div class="layer-form" style="padding: 20px;">' +
                                            '<div class="form-group">' +
                                            '<label for="price-input">本地车回收价(元/吨):</label>' +
                                            '<input type="number" id="price-input" class="form-control" placeholder="请输入本地车回收价(元/吨)" value="' + (row.senddesc || '') + '">' +
                                            '</div>' +
                                            '<div class="form-group">' +
                                            '<label for="remark-input">外地车回收价(元/吨):</label>' +
                                            '<input type="text" id="remark-input" class="form-control" placeholder="请输入外地车回收价(元/吨)">' +
                                            '</div>' +
                                            '</div>';

                                        // 使用Layer.open创建自定义弹出层
                                        Layer.open({
                                            type: 1,
                                            title: "报价",
                                            area: ['500px', '300px'], // 调整弹出层大小
                                            content: content,
                                            btn: ['确定', '取消'],
                                            yes: function(index, layero) {
                                                var price = $('#price-input').val();
                                                var yprice = $('#remark-input').val();

                                                if (!price) {
                                                    Layer.msg('请输入单价');
                                                    return;
                                                }

                                                Fast.api.ajax({
                                                    url: "backprice/do_price?priceid=" + row.id,
                                                    data: {
                                                        money: price,
                                                        ymoney: yprice // 新增的备注字段
                                                    },
                                                }, function (data, ret) {
                                                    Layer.close(index);
                                                    $(".btn-refresh").trigger("click");
                                                });
                                            },
                                            btn2: function(index, layero) {
                                                // 取消按钮回调
                                            },
                                            success: function(layero, index) {
                                                // 弹出层显示成功后的回调
                                                // 可以在这里添加一些初始化逻辑
                                            }
                                        });

                                        return false;
                                    }
                                }
                            ], formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            $("#do_status").click(function(){
                var ids = Table.api.selectedids(table);

                Fast.api.open('backprice/do_status?ids='+ids, '批量录入', {area:["80%", "80%"]});
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        do_status: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
